{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python绘制预算开销雷达图"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "***雷达图（Radar）：***  \n",
    "* 又称为蜘蛛图、极地图、星图\n",
    "* 从同一点开始的轴上表示的两个或更多个变量的二维图表的形式显示**多变量数据**的图形方法。\n",
    "\n",
    "雷达图主要应用于企业经营状况——收益性、生产性、流动性、安全性和成长性的评价。\n",
    "\n",
    "***缺点：***  \n",
    "如果多边形过多，就会显得非常混乱，所以一般展示一到两个变量多边形"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Radar"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 数据统计结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 部门列表\n",
    "departments = ['销售', '管理', '信息技术', '客服', '研发', '市场']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 容忍的最大支出\n",
    "max_cost_threshold = [6500, 16000, 30000, 38000, 52000, 25000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 预算分配\n",
    "budget_allocation = [4300, 10000, 28000, 35000, 50000, 19000]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实际开销\n",
    "actual_cost = [5000, 14000, 28000, 31000, 42000, 21000]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 绘制雷达图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置雷达图的边缘的最大值\n",
    "schema = []\n",
    "for dept, max_cost in zip(departments, max_cost_threshold):\n",
    "    schema.append(opts.RadarIndicatorItem(name=dept, max_=max_cost))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<pyecharts.options.global_options.RadarIndicatorItem at 0x13257b9d4c8>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x13257b9d508>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x13257b9d548>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x13257b9d588>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x13257b9d5c8>,\n",
       " <pyecharts.options.global_options.RadarIndicatorItem at 0x13257b9d648>]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "schema"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"0052660c5c5040fa89d3bba8ec5a3172\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_0052660c5c5040fa89d3bba8ec5a3172 = echarts.init(\n",
       "                    document.getElementById('0052660c5c5040fa89d3bba8ec5a3172'), 'white', {renderer: 'canvas'});\n",
       "                var option_0052660c5c5040fa89d3bba8ec5a3172 = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"radar\",\n",
       "            \"name\": \"\\u9884\\u7b97\\u5206\\u914d\",\n",
       "            \"data\": [\n",
       "                [\n",
       "                    4300,\n",
       "                    10000,\n",
       "                    28000,\n",
       "                    35000,\n",
       "                    50000,\n",
       "                    19000\n",
       "                ]\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": false,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"itemStyle\": {\n",
       "                \"normal\": {}\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\",\n",
       "                \"color\": \"#FF0000\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        },\n",
       "        {\n",
       "            \"type\": \"radar\",\n",
       "            \"name\": \"\\u5b9e\\u9645\\u5f00\\u9500\",\n",
       "            \"data\": [\n",
       "                [\n",
       "                    5000,\n",
       "                    14000,\n",
       "                    28000,\n",
       "                    31000,\n",
       "                    42000,\n",
       "                    21000\n",
       "                ]\n",
       "            ],\n",
       "            \"label\": {\n",
       "                \"show\": false,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8\n",
       "            },\n",
       "            \"itemStyle\": {\n",
       "                \"normal\": {}\n",
       "            },\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\",\n",
       "                \"color\": \"#0000FF\"\n",
       "            },\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            },\n",
       "            \"rippleEffect\": {\n",
       "                \"show\": true,\n",
       "                \"brushType\": \"stroke\",\n",
       "                \"scale\": 2.5,\n",
       "                \"period\": 4\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [\n",
       "                \"\\u9884\\u7b97\\u5206\\u914d\",\n",
       "                \"\\u5b9e\\u9645\\u5f00\\u9500\"\n",
       "            ],\n",
       "            \"selected\": {\n",
       "                \"\\u9884\\u7b97\\u5206\\u914d\": true,\n",
       "                \"\\u5b9e\\u9645\\u5f00\\u9500\": true\n",
       "            },\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0\n",
       "    },\n",
       "    \"radar\": {\n",
       "        \"indicator\": [\n",
       "            {\n",
       "                \"name\": \"\\u9500\\u552e\",\n",
       "                \"max\": 6500\n",
       "            },\n",
       "            {\n",
       "                \"name\": \"\\u7ba1\\u7406\",\n",
       "                \"max\": 16000\n",
       "            },\n",
       "            {\n",
       "                \"name\": \"\\u4fe1\\u606f\\u6280\\u672f\",\n",
       "                \"max\": 30000\n",
       "            },\n",
       "            {\n",
       "                \"name\": \"\\u5ba2\\u670d\",\n",
       "                \"max\": 38000\n",
       "            },\n",
       "            {\n",
       "                \"name\": \"\\u7814\\u53d1\",\n",
       "                \"max\": 52000\n",
       "            },\n",
       "            {\n",
       "                \"name\": \"\\u5e02\\u573a\",\n",
       "                \"max\": 25000\n",
       "            }\n",
       "        ],\n",
       "        \"name\": {\n",
       "            \"textStyle\": {}\n",
       "        },\n",
       "        \"splitLine\": {\n",
       "            \"show\": true,\n",
       "            \"lineStyle\": {\n",
       "                \"show\": true,\n",
       "                \"width\": 1,\n",
       "                \"opacity\": 1,\n",
       "                \"curveness\": 0,\n",
       "                \"type\": \"solid\"\n",
       "            }\n",
       "        },\n",
       "        \"splitArea\": {\n",
       "            \"show\": true,\n",
       "            \"areaStyle\": {\n",
       "                \"opacity\": 0\n",
       "            }\n",
       "        },\n",
       "        \"axisLine\": {\n",
       "            \"show\": true,\n",
       "            \"onZero\": true,\n",
       "            \"onZeroAxisIndex\": 0\n",
       "        }\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5404\\u90e8\\u95e8\\u9884\\u7b97\\u4e0e\\u5f00\\u9500\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_0052660c5c5040fa89d3bba8ec5a3172.setOption(option_0052660c5c5040fa89d3bba8ec5a3172);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x13257ba9d48>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "radar = (\n",
    "    Radar()\n",
    "    .add_schema(schema)\n",
    "    .add(\"预算分配\", [budget_allocation], linestyle_opts=opts.LineStyleOpts(color=\"#FF0000\"))\n",
    "    .add(\"实际开销\", [actual_cost], linestyle_opts=opts.LineStyleOpts(color=\"#0000FF\"))\n",
    "    .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "    .set_global_opts(\n",
    "        #legend_opts=opts.LegendOpts(selected_mode=\"single\"),\n",
    "        title_opts=opts.TitleOpts(title=\"各部门预算与开销\"),\n",
    "    )\n",
    ")\n",
    "\n",
    "radar.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
